package com.hb.software.register.impl;

import com.hb.software.entity.user.User;
import com.hb.software.entity.worker.Worker;
import com.hb.software.register.RegisterService;
import com.hb.software.register.dao.RegisterDao;
import com.hb.software.worker.dao.WorkerDao;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class RegisterServiceImpl implements RegisterService {

    private static final Logger logger = LogManager.getLogger(RegisterServiceImpl.class.getName());

    @Autowired
    private RegisterDao registerDao;

    @Autowired
    private WorkerDao workerDao;


    @Override
    public String selectAdminnum() {
        return  registerDao.selectAdminnum();
    }

    @Override
    public void updateAdminnum(String adminnum){
        registerDao.updateAdminnum(adminnum);
    }

    @Override
    public Long findLastId(){
        return registerDao.findLastId();
    }

    @Override
    public void register(User user) {
        if (registerDao.findByUsername(user.getUsername()) == null) {
            user.setId(this.findLastId()+1);
            Worker wo = new Worker();
            wo.setId(workerDao.findLastId()+1);
            workerDao.save(wo);
            registerDao.save(user);
        }else{
            logger.debug("用户名已经存在~~~~");
        }
    }

    @Override
    public User findById(Long id) {
        return registerDao.findById(id);
    }
}
